/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.ManufacturerDao;
import com.techshop.model.Manufacturer;
import com.techshop.util.MyUtil;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Hai-PC
 */
public class ManufacturerDaoImpl extends HibernateDaoSupport implements ManufacturerDao
{
    
    @Override
    public Manufacturer getById(int manufacturerID)
    {
        try
        {
            return (Manufacturer) getSession().get(Manufacturer.class, manufacturerID);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error:", e.toString());
            return null;
        }
    }

    @Override
    public List<Manufacturer> findAll()
    {
         System.out.println("ManufacturerDaoImpl.findAll");
        try
        {
            return getSession().createQuery("from Manufacturer").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error:", e.toString());
            return null;
        }
    }
    
    @Override
    public boolean insert(Manufacturer manufaceturer)
    {
        try
        {
            getHibernateTemplate().save(manufaceturer);
            return true;
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error:", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(Manufacturer manufacturer)
    {
        try
        {
            if (getById(manufacturer.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(manufacturer));
                return true;
            }
        }
        catch (Exception e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Manufacturer manufacturer)
    {
        try
        {
            if (getById(manufacturer.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(manufacturer));
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}